package us.codecraft.demo.pipeline;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import us.codecraft.demo.model.DouBanFilm;
import us.codecraft.demo.service.DouBanFilmService;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

public class DouBanProcessor implements PageProcessor {

	private ApplicationContext context;
	
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(3000)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
    private static final String preFixUrl="http://blog.csdn.net/?&page="; 
	public DouBanProcessor() {
        context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    }
	@Autowired
	private DouBanFilmService douBanFilmService;
	public Site getSite() {
		return site;
	}

	public void process(Page page) {
		 
		List<String> list = page.getHtml().xpath("div[@class='blog_list_wrap']").all();
		for (int i = 0; i < list.size(); i++) {
			Html h = new Html(list.get(i));
			String anthour=h.xpath("//dl[@class='blog_list clearfix']/dt/a[@class='nickname']/text()").toString();
			String title=h.xpath("//dl[@class='blog_list clearfix']/dd/h3/a/text()").toString();
			String content=h.xpath("//dl[@class='blog_list clearfix']/dd/div[1]/text()").toString();
			String time=h.xpath("//dl[@class='blog_list clearfix']/dd/div[@class='blog_list_b clearfix']/div[@class='blog_list_b_r fr']/label/text()").toString();
			System.out.println("作者："+anthour);
			System.out.println("标签："+title);
			System.out.println("内容："+content);
			System.out.println("时间："+time);
			douBanFilmService = (DouBanFilmService) context.getBean("douBanFilmService");
			DouBanFilm banFilm=new DouBanFilm();
			banFilm.setFilmName(title);
			banFilm.setType(content);
			banFilm.setFilmGrade(time);
			douBanFilmService.insert(banFilm);
		}
	}

	public static void main(String[] args) {
		List<String> urls=new ArrayList<String>(); 
		String newUrl="";
		for (int i = 1; i < 400; i++) {
			newUrl=preFixUrl+i;
			System.out.println(newUrl);
			urls.add(newUrl);
		}
		DouBanProcessor processor = new DouBanProcessor();
        Spider.create(processor)
                .startUrls(urls)
                .thread(10)
                .runAsync();

	}

}
